<?php

namespace app\home\home;
use app\admin\controller\Admin;
use app\home\model\Gift;
use app\home\model\User;
use think\Controller;
use think\Db;
use think\Exception;
use think\Loader;
use think\Request;
use app\common\model\AdminAnnex as AnnexModel;
class Gifts extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {
        $data = $request->param();
        $gift = new Gift();
        $res = $gift->lists($data);
        if ($res){
            return apistatus(1,'列表获取成功',$res);
        }else{
            return apistatus(0,'无数据');
        }

    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create(Request $request)
    {
        $data = $request->param();
//        self::checktoken($data['id']);
        $info = \db('home_users')->where('id',$data['id'])->field('rd_coin')->find();
        if (empty($info['rd_coin']) || bccomp($info['rd_coin'],$data['cost'],2) < 0) {
            return apistatus(1, '红豆币余额不足', ['has_coin' => 0]);
        }
            $insert = [
                'uid' => $data['id'],
                'givenid' => $data['toid'],
                'create_time' => time(),
                'giftid' => $data['giftid'],
            ];
            $f = Db::table('rd_home_giftrecord')->insertGetId($insert);
            $costinsert = [
                'uid'=>$data['id'],
                'toid'=>$data['toid'],
                'giftid'=>$data['giftid'],
                'num'=>$data['cost'],
                'create_time'=>time(),
                'cost_type'=>3,

            ];
            $userupdate = [
                'id'=>$data['id'],
                'rd_coin'=>bcsub($info['rd_coin'],$data['cost'],2),
            ];
            $t = Db::table('rd_home_coinrecord')->insertGetId($costinsert);
            if ($f && $t){
                    Db::startTrans();
                    try{
                        Db::table('rd_home_users')->update($userupdate);
                        Db::table('rd_home_giftrecord')->update(['id'=>$f,'status'=>2]);
                        Db::table('rd_home_coinrecord')->update(['id'=>$t,'status'=>2]);
                        // 提交事务
                        Db::commit();
                        return apistatus(1,'赠送成功');
                    } catch (\Exception $e) {
                        // 回滚事务
                        return apistatus(0,'赠送失败');
                        Db::rollback();
                    }
            }else{
                return apistatus(0,'操作失败');
            }

        }



    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //

    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    public function mygift(Request $request)
    {
        $data = $request->param();
//        self::checktoken($data['id']);
        isset($data['page']) ? $page = $data['page'] : $page = 1;
        isset($data['listrow']) ? $listrow = $data['listrow'] : $listrow = 10;
        if ($data['type'] == 1){
            $where = [
                'G.givenid' => $data['id'],
                'G.status' => 2,
            ];
            $res = \db('home_giftrecord')->alias('G')
                ->join('home_users U','G.uid=U.id')
                ->join('home_gifts F','G.giftid=F.id')
                ->where($where)
                ->field('G.id,G.giftid,U.nickname,F.giftname,F.cost')
                ->paginate($listrow,true,['page'=>$page]);
        }else{
            $where = [
                'G.uid' => $data['id'],
                'G.status' => 2,
            ];
            $res = \db('home_giftrecord')->alias('G')
                ->join('home_users U','G.givenid=U.id')
                ->join('home_gifts F','G.giftid=F.id')
                ->where($where)
                ->field('G.id,G.giftid,U.nickname,F.giftname,F.cost')
                ->paginate($listrow,true,['page'=>$page]);
        }
        if ($res){
            return apistatus(1,'列表获取成功',$res);
        }else{
            return apistatus(0,'无数据');
        }

    }

    

}
